package com.kgc.crowd.ossdemo.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

/**
 * 项目名：distribution-crowd-parent
 * 创建人：Junyizm
 * 创建时间：2020/3/26-10:53
 */
public class AliyunOSSUtil {

    public static String upload(InputStream inputStream,String oldfileName){
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI4FpX9YQhAMdXhepc5mD6";
        String accessKeySecret = "dI3pHm0MyAl0i72n5xq5I7usVmJfgF";
        String folderName = "test";
        String bucketName = "bucket-qwe";

        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        String newFileName = UUID.randomUUID().toString().replaceAll("-","")+oldfileName;
        String fileURL = folderName + "/" + newFileName;
        try {
            ossClient.putObject(bucketName,fileURL,inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
        return fileURL;
    }
}
